<?php

	// +----------------------------------------------------------------------
	// | 店铺管理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\store\admin;

	use app\model\store\StoreApp as StoreAppModel;
	use app\event\store\Store as StoreEvent;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;

	class StoreApp extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
            $map[] = ['status', '>', -2];
            $map[] = ['store_id', '=', $this->store['id']];
			return $map;
		}

		/**
		 * 商品评论
		 * @return mixed|string
		 * @access public
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function index()
		{
			// 显示页面

			$builder = new Table();
			$builder->model(StoreAppModel::class)
				->where($this->_filter())
				->order('id DESC')
                ->buttonUpdate()
//				->searchText('nickname', '商家昵称', '商家昵称')
				//             ->buttonUpdate(url('card/admin/Store/update'))
				//             ->searchSelect('status', '状态',$statusOptions)
				->keyId()
//				->keyHidden('code')
//				->keyLink('nickname', '店铺昵称', '/store/{{d.code}}', '_block')
				->keyText('name', '名称')
				->keyText('website', '站点')
				->keyText('app_key', 'app_key')
//				->keyText('seats', '坐席数')
//				->keyText('grade.name', '店铺等级')
//				->keyText('contacts', '联系人')
//				->keyText('mobile', '联系方式')
//				->keyText('order_commission', '手续费')
//				->keyBool('is_open', '是否可用')
//				->keyTime('end_time', '到期时间')
				->keyCreateTime()
				->keyStatus()
				->actionUpdate('link?id={$id}', '部署')
//				->actionUpdate('credit?id={$id}', '保证金', [-1, 0, 1])
				->actionUpdate()
				->actionForbid()
				->actionRestore();
			return $builder->fetch();
		}

        public function link()
        {

            $app = StoreAppModel::where('id',$this->request->param('id'))->find();
            if (!$app)
            {
                $this->_returnError( '应用不存在');
            }
            $lists = [];
            // 客服
            $service = \app\model\ucenter\User::where('store_id', $this->user['store_id'])->where('type', 1)->where('is_open', 1)->select();
            $param = [];
            $param['app_key'] = $app['app_key'];
            foreach ($service as $index => $item) {
                $param['app_key'] =$app['app_key'];
                $param['service_id'] = $item['id'];
                $lists[] = [
                    'name' => $item['nickname'],
                    'url' => (string)url('/start', $param, true, true)
                ];
            }
            $this->assign('app_key', $app['app_key']);
            $this->assign('store', $this->store);
//            $basename = $this->request->root();
//            if (pathinfo($basename, PATHINFO_EXTENSION) == 'php') {
//                $basename = dirname($basename);
//            }
//			$this->assign('basename', $basename);
            $this->assign('lists', $lists);
            $this->assign('services', $service);
            $this->assign('web',$this->request->domain());
            $this->assign('html2', htmlspecialchars($this->fetch('form')));
            $this->assign('js', htmlspecialchars($this->fetch('script')));
            $this->assign('root_url', $this->request->root(true));
            return $this->fetch();
        }
		/**
		 * 编辑店铺信息
		 * @return mixed|string
		 * @access public
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function update()
		{
			if ($this->request->isPost()) {

				parent::_update();
			} else {
				$id = $this->request->get('id');
				if ($id) {
                    $data = StoreAppModel::where('id', $id)->where('store_id',$this->store['id'])->find();
				}else{
                    $data = [];
                    $data['store_id'] = $this->store['id'];
                }
				$builder = new Edit();
				$builder->title('编辑应用信息')
                    ->keyHidden('store_id')
                    ->keyTextInline('name', '名称')
                    ->keyText('website', '站点')
                    ->keyNumber('seats', '坐席数')
                    ->keyNumber('service_number', '同时服务人数')
                    ->keyNumber('chat_timeout_close', '超时关闭时间')
                    ->keyNumber('chat_timeout_tips', '超时提示时间')
                    ->keyNumber('seats', '坐席数')
					->data($data)
//					->explain('隐藏库存：库存大于100，显示“库存非常多”、小于100、大于30，显示“库存很多”、小于30、大于10，显示“库存一般”、小于10，显示“库存少量”')
					->buttonSubmit();
				return $builder->fetch();
			}
		}

	  

		/**
		 * 删除消息
		 */
		public function forbid()
		{
			$message = StoreAppModel::where('id', $this->request->param('id'))
				->find();
			if ($message) {
				$message['status'] = -1;
				$message->save();
				$this->_returnSuccess( '禁用成功');
			} else {
				$this->_returnSuccess( '禁用失败');
			}
		}
		/**
		 * 删除消息
		 */
		public function restore()
		{
			$message = StoreAppModel::where('id', $this->request->param('id'))
				->find();
			if ($message) {
				$message['status'] = 1;
				$message->save();
				$this->_returnSuccess( '恢复成功');
			} else {
				$this->_returnSuccess( '恢复失败');
			}
		}
	}
